package com.seng.loginservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seng.loginservice.entity.TbLog;
import com.seng.loginservice.service.TbLogService;
import com.seng.utilscommon.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 操作日志表 前端控制器
 * </p>
 *
 * @author seng
 * @since 2020-07-31
 */
@Api(value = "操作日志controller类")
@CrossOrigin
@RestController
@RequestMapping("/logservice/tb-log")
public class TbLogController {

    @Autowired
    private TbLogService tbLogService;

    /**
     *
     * @param current 当前页数
     * @param size  当前条数
     * @param tbLog 当前模糊查询对象
     * @return
     */
    @ApiOperation(value = "查询所有日志信息")
    @PostMapping("/findAll/{current}/{size}")
    public Result findAll(@PathVariable("current") Integer current,
                          @PathVariable("size") Integer size,
                          @RequestBody(required = false) TbLog tbLog) {

        System.out.println(tbLog.toString());

        Page<TbLog> page = new Page<>(current, size);
        QueryWrapper<TbLog> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(tbLog.getUsername())) {
            queryWrapper.like("username", tbLog.getUsername());
        }
        if (!StringUtils.isEmpty(tbLog.getIp())) {
            queryWrapper.like("ip", tbLog.getIp());
        }
        if (!StringUtils.isEmpty(tbLog.getLocation())) {
            queryWrapper.like("location", tbLog.getLocation());
        }
        IPage<TbLog> loglogPage = tbLogService.page(page, queryWrapper);
        return Result.ok().data("loglog", loglogPage);
    }

    @ApiOperation(value = "逻辑删除用户")
    @DeleteMapping("deleteLoglogById/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        System.out.println(id);
        boolean delete = tbLogService.removeById(id);
        if (delete) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @ApiOperation(value = "批量删除用户")
    @PostMapping("deleteLoglogByIdList")
    public Result deleteList(@RequestBody(required = false) List<Long> ids) {
        if (tbLogService.removeByIds(ids)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

}

